package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import counter.com.baomidou.entity.Datalinkinfo;
import counter.com.baomidou.mapper.DatalinkinfoMapper;
import counter.com.baomidou.service.IDatalinkinfoService;
import counter.com.common.Result;
import counter.com.connproperty.callback.CallbackStatisticsResponseProperty;
import counter.com.connproperty.datalink.AddDatalinkInfoProperty;
import counter.com.connproperty.datalink.ChangeDatalinkStatusProperty;
import counter.com.connproperty.datalink.DatalinkPageProperty;
import counter.com.connproperty.fileinfo.DatalinkInfoDeleteProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-09-28
 */
@RestController
@RequestMapping("/datalinkinfo")
public class DatalinkinfoController {

    @Autowired
    DatalinkinfoMapper datalinkinfoMapper;

    @Autowired
    IDatalinkinfoService datalinkinfoService;
    private static Integer OFFLINE = 0;
    private static Integer ONLINE = 1;

    @PostMapping("/search")
    public Result search(@RequestBody DatalinkPageProperty data) {
        LambdaQueryWrapper<Datalinkinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Datalinkinfo::getId);
        Page page = new Page(data.getPage(), data.getPagesize());
        IPage iPage = datalinkinfoService.page(page, wrapper);
        return Result.success(iPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated AddDatalinkInfoProperty data) {
        Datalinkinfo info = new Datalinkinfo();
        info.setIp(data.getIp());
        info.setLocation(data.getLocation());
        info.setVpsstatus(OFFLINE);
        info.setCreatedtime(LocalDateTime.now());
        datalinkinfoMapper.insert(info);
        return Result.success(null);
    }

    @PostMapping("/change")
    public Result change(@RequestBody ChangeDatalinkStatusProperty data) {
        LambdaQueryWrapper<Datalinkinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Datalinkinfo::getId, data.getId());
        Datalinkinfo datalinkinfo = datalinkinfoService.getOne(wrapper);
        if (datalinkinfo == null) {
            return Result.fail("改变异常");
        } else {
            datalinkinfo.setVpsstatus(data.getStatus());
            datalinkinfoMapper.updateById(datalinkinfo);
            return Result.success(datalinkinfo);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody DatalinkInfoDeleteProperty data) {
        datalinkinfoService.removeById(data.getId());
        return Result.success("删除成功");
    }

    /***
     * Return
     * total -- int
     * onlinetotal -- int
     * offlinetotal -- int
     * locationtotal -- int
     *分别对应VPS总数，在线总数，离线总数，位置总数
     */
    @PostMapping("/statistics")
    public Result statistics() {
        LambdaQueryWrapper<Datalinkinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Datalinkinfo::getVpsstatus, ONLINE);
        int countOnline = datalinkinfoService.count(wrapper);
        wrapper.clear();
        wrapper.eq(Datalinkinfo::getVpsstatus, OFFLINE);
        int countOffline = datalinkinfoService.count(wrapper);
        int ONLINE_TOTAL = countOnline;
        int OFFLINE_TOTAL = countOffline;
        return Result.success(new CallbackStatisticsResponseProperty(
                countOnline + countOffline,
                countOnline,
                countOffline,
                0
        ));
    }
}
